package org.manon.json.asm;

import java.util.LinkedHashMap;
import java.util.Map;

import org.manon.json.JSONException;
import org.manon.json.analyse.ClassRegistry;
import org.manon.json.meta.Entity;
import org.objectweb.asm.ClassWriter;

/**
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 */
public abstract class ASMBaseGenerator {

	/**
	 * Holder of all Entity and ClassWriter.
	 */
	private final Map<Entity,ClassWriter> _entities = new LinkedHashMap<Entity, ClassWriter>();
	
	private final ClassRegistry _registry;
	
	ASMBaseGenerator(ClassRegistry classRegistry) {
		_registry = classRegistry;
	}
	
	protected Map<Entity, ClassWriter> getAllClasses() {
		return _entities;
	}
	
	public Entity findEntityClass(Class<?> entityClass) {
		for (Entity entity : _entities.keySet()) {
			if (entityClass.equals(entity.getEntityClass())) {
				return entity;
			}
		}
		throw new JSONException("No Entity for class [" + entityClass + "]");
	}

	public ClassRegistry getRegistry() {
		return _registry;
	}
	
}